import xml.etree.ElementTree as ET
import os
import re
 
classes = ['cola','gum','sprite','fanta','water']
 
p = re.compile(r'(.*)\.')
for i in os.listdir('/home/hdkj/Desktop/A01/Annotations'):
    i = p.findall(i)[0]
    xml_file = '/home/hdkj/Desktop/A01/Annotations/{}.xml'.format(i)
    tree = ET.parse(xml_file)
    root = tree.getroot()
 
    for size in root.iter('size'):
        width = int(size.find('width').text)
        height = int(size.find('height').text)
 
    all_content = ''
    for obj in root.iter('object'):
        cls = obj.find('name').text
        cls_id = classes.index(cls)
 
        xmlbox = obj.find('bndbox')
        xmin = int(xmlbox.find('xmin').text)
        ymin = int(xmlbox.find('ymin').text)
        xmax = int(xmlbox.find('*****').text)
        ymax = int(xmlbox.find('*****').text)
 
        x = (xmin+xmax)/2/width
        y = (ymin+ymax)/2/height
        w = (xmax-xmin)/width
        h = (ymax-ymin)/height
 
        one_content = '{} {:.4f} {:.4f} {:.4f} {:.4f}'.format(cls_id,x,y,w,h)
        all_content = all_content + one_content + '\n'
 
    all_content = all_content.strip('\n')
    file = open('*****'.format(i),'w')
    print(all_content,file=file,flush=True)
